#============================================================
#	Item Panel
#============================================================
# @datetime: 2022-4-20 23:43:27
#============================================================
class_name HUD_ItemPanel
extends Control


##  点击返回按钮
signal back


# 物品数据中类型的 Key 名
const ITEM_TYPE_KEY : String = "type"


onready var _equipment := get_node("VBoxContainer/HBoxContainer/Equipment") as HUD_Equipment
onready var _item_grid := get_node("VBoxContainer/HBoxContainer/ItemGrid") as HUD_ItemGrid
onready var _state_list := get_node("VBoxContainer/MarginContainer/HBoxContainer2/StateList") as HUD_StateList


var item_wrapper : ItemWrapper
var equipment_wrapper : EquipmentWrapper
var property_wrapper : PropertyWrapper


#============================================================
#   内置
#============================================================
func _ready():
	var _player = Global.player
	
	# 初始化更新显示数据
	property_wrapper = WrapperHelper.get_property(_player)
	property_wrapper.connect("property_changed", self, "_property_changed")
	var p_data = property_wrapper.get_property_data()
	for prop in p_data:
		_property_changed(prop, p_data[prop])
	
	item_wrapper = WrapperHelper.get_item(_player)
	item_wrapper.connect("item_event", self, "_item_event")
	
	equipment_wrapper = WrapperHelper.get_equipment(_player)
	equipment_wrapper.set_type_key(ITEM_TYPE_KEY)


#============================================================
#   连接信号
#============================================================
# 属性改变
func _property_changed(property, value):
	if property == 'health':
		_state_list._health.text = str(value)


# 物品操作
func _item_event(item: Dictionary, id: String, event):
#	Logger.error(self, ['---------------- ', event])
	match event:
		ItemWrapper.Add:
			_item_grid.add_item(item)
		ItemWrapper.Remove:
			_item_grid.remove_item(item)
		ItemWrapper.Change:
			pass


# 使用物品
func _use_item(item_data: Dictionary):
	match item_data.get(ITEM_TYPE_KEY):
		# 装备武器
		ItemBean.Type.WEAPON:
			if not equipment_wrapper.is_loaded(item_data, ['count']):
				# 取出一件这个物品
				var item = item_wrapper.take_out(item_data['id'])
				# 加载到装备上
				equipment_wrapper.load_item(item, item_data[ITEM_TYPE_KEY])
			else:
				Logger.info(self, ["已经装备过这个物品了"])
		
		# 消耗品
		ItemBean.Type.CONSUMABLES:
			var item = item_wrapper.take_out(item_data['id'])
			property_wrapper.add_property_data(item)
			Logger.error(self, ["消耗品：", item])



# 点击返回
func _on_Back_pressed():
	emit_signal("back")


